package esay.array;

/**
 * 将矩阵旋转90度
 * @author ruizhou
 * @date 26.02.2018
 */
public class RotateImage {
    private static void rotate(int[][] matrix) {
        int len = matrix.length;
        if (len < 2) {
            return;
        }

        for (int i = 0,size = len/2; i < size; i++) {
            for (int j = i, last = len-i-1; j < last; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[len-j-1][i];
                matrix[len-j-1][i] = matrix[len -i-1][len -j-1];
                matrix[len -i-1][len -j-1] = matrix[j][len-i-1];
                matrix[j][len-i-1] = temp;
            }
        }
    }

    public static void main(String[] args) {
//        int[][] matrix = {{1,2,3},
//                          {4,5,6},
//                          {7,8,9}};
        int[][] matrix = {{1,  2 ,  3  ,4},
                          {5,  6 ,  7  ,8},
                          {9,  10,  11 ,12},
                          {13, 14,  15 ,16}};
        rotate(matrix);
    }
}
